#include "expcollectionparaframe.h"
#include "ui_expcollectionparaframe.h"
#include "sourcemanage.h"
#include "delegate.h"
#include "algorithm.h"

ExpCollectionParaFrame::ExpCollectionParaFrame(QWidget *parent,Experiment* pExperiment) :
    QFrame(parent),
    ui(new Ui::ExpCollectionParaFrame)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;
    pPreMyScanAxis=new MyScanAxis;
    pMainMyScanAxis=new MyScanAxis;
    ui->verticalLayout_Pre->addWidget(pPreMyScanAxis);
    ui->verticalLayout_Main->addWidget(pMainMyScanAxis);

    LineEditDelegate *pLineEditDelegate=new LineEditDelegate(this,0);
    ui->tableWidget_PreScan->setItemDelegateForColumn(1,pLineEditDelegate);
    pLineEditDelegate=new LineEditDelegate(this,0);
    ui->tableWidget_PreScan->setItemDelegateForColumn(2,pLineEditDelegate);
    pLineEditDelegate=new LineEditDelegate(this,0);
    ui->tableWidget_PreScan->setItemDelegateForColumn(3,pLineEditDelegate);
    pLineEditDelegate=new LineEditDelegate(this,3);
    ui->tableWidget_PreScan->setItemDelegateForColumn(4,pLineEditDelegate);
    ComboxDelegate *pComboxDelegate=new ComboxDelegate(this,0);
    ui->tableWidget_PreScan->setItemDelegateForColumn(5,pComboxDelegate);

    pLineEditDelegate=new LineEditDelegate(this,3);
    ui->tableWidget_Hop->setItemDelegateForColumn(2,pLineEditDelegate);
    pLineEditDelegate=new LineEditDelegate(this,3);
    ui->tableWidget_Hop->setItemDelegateForColumn(3,pLineEditDelegate);
    pLineEditDelegate=new LineEditDelegate(this,0);
    ui->tableWidget_Hop->setItemDelegateForColumn(4,pLineEditDelegate);
    pComboxDelegate=new ComboxDelegate(this,0);
    ui->tableWidget_Hop->setItemDelegateForColumn(5,pComboxDelegate);

    pLineEditDelegate=new LineEditDelegate(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(1,pLineEditDelegate);
    pLineEditDelegate=new LineEditDelegate(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(2,pLineEditDelegate);
    pLineEditDelegate=new LineEditDelegate(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(3,pLineEditDelegate);
    pLineEditDelegate=new LineEditDelegate(this,3);
    ui->tableWidget_Sweep->setItemDelegateForColumn(4,pLineEditDelegate);
    pComboxDelegate=new ComboxDelegate(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(5,pComboxDelegate);

    ui->tableWidget_PreScan->setColumnHidden(0,true);
    ui->tableWidget_Hop->setColumnHidden(1,true);
    ui->tableWidget_Sweep->setColumnHidden(0,true);

    connect(ui->checkBox_PreScan,SIGNAL(stateChanged(int)),this,SIGNAL(SIG_DataChanged()));
    connect(ui->spinBox_PreCount,SIGNAL(editingFinished()),this,SIGNAL(SIG_DataChanged()));
    connect(ui->spinBox__MainCount,SIGNAL(editingFinished()),this,SIGNAL(SIG_DataChanged()));
    connect(ui->radioButton_SweepScan,SIGNAL(clicked()),this,SIGNAL(SIG_DataChanged()));
    connect(ui->radioButton_HopScan,SIGNAL(clicked()),this,SIGNAL(SIG_DataChanged()));
    connect(this,SIGNAL(SIG_DataChanged()),this,SLOT(OnDataChanged()));
    LoadData();
}

ExpCollectionParaFrame::~ExpCollectionParaFrame()
{
    delete ui;
}

void ExpCollectionParaFrame::LoadData()
{
    disconnect(ui->tableWidget_PreScan,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_PreScan_itemChanged(QTableWidgetItem*)));
    disconnect(ui->tableWidget_Hop,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Hop_itemChanged(QTableWidgetItem*)));
    disconnect(ui->tableWidget_Sweep,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Sweep_itemChanged(QTableWidgetItem*)));
    disconnect(this,SIGNAL(SIG_DataChanged()),this,SLOT(OnDataChanged()));

    pAnalyteDataBase=pExperiment->GetAnalyteDataBase();
    ui->tableWidget_Forbid->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QList<QPair<double,double> > forbidRangs=pAnalyteDataBase->GetForbidRangs();
    int count=forbidRangs.count();
    ui->tableWidget_Forbid->setRowCount(count);
    for(int row=0;row<count;row++)
    {
        int column=0;
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(forbidRangs.at(row).first));
        ui->tableWidget_Forbid->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(forbidRangs.at(row).second));
        ui->tableWidget_Forbid->setItem(row,column++,pItem);
    }

    pExpConllectPara=pExperiment->GetExpConllectParaPtr();
    count=pExpConllectPara->GetPreSweepScanParas().count();
    if(count<1)//
    {
        QList<QPair<double,double> > enableRangs=pAnalyteDataBase->GetEnableRangs();
        pExpConllectPara->InitPreScanPara(enableRangs);
        count=enableRangs.count();
    }

    ui->tableWidget_PreScan->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QList<SweepScanPara> preSweepScanParas=pExpConllectPara->GetPreSweepScanParas();
    count=preSweepScanParas.count();
    ui->tableWidget_PreScan->setRowCount(count);
    for(int row=0;row<count;row++)
    {
        SweepScanPara sweepScanPara=preSweepScanParas.at(row);
        int column=0;
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(row));
        ui->tableWidget_PreScan->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.startMassNum));
        ui->tableWidget_PreScan->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.endMassNum));
        ui->tableWidget_PreScan->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.dwelltime));
        ui->tableWidget_PreScan->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.channelsAmu));
        ui->tableWidget_PreScan->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sweepScanPara.type);
        ui->tableWidget_PreScan->setItem(row,column++,pItem);
    }
    ui->tableWidget_PreScan->sortByColumn(1,Qt::AscendingOrder);

    //
    ui->tableWidget_Sweep->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QHash<QString,SweepScanPara> sweepScanParas=pExpConllectPara->GetSweepScanParas();
    count=sweepScanParas.count();
    ui->tableWidget_Sweep->setRowCount(count);
    int row=0;
    foreach(SweepScanPara sweepScanPara,sweepScanParas)
    {
        int column=0;
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText(sweepScanPara.name);
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.startMassNum));
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.endMassNum));
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.dwelltime));
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.channelsAmu));     
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sweepScanPara.type);
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        row++;
    }
    ui->tableWidget_Sweep->sortByColumn(1,Qt::AscendingOrder);

    ui->tableWidget_Hop->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QHash<QString,HopScanPara> hopScanParas=pExpConllectPara->GetHopScanParas();
    count=hopScanParas.count();
    ui->tableWidget_Hop->setRowCount(count);
    row=0;
    foreach(HopScanPara hopScanPara,hopScanParas)
    {
        int column=0;
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
        pItem->setText(hopScanPara.name);//
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.massNum));//
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.dwelltime));
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.chanels));
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.interval));
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(hopScanPara.type);
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        row++;
    }
    ui->tableWidget_Hop->sortByColumn(1,Qt::AscendingOrder);

    ui->checkBox_PreScan->setChecked(pExpConllectPara->IsPreScan());
    on_checkBox_PreScan_clicked(pExpConllectPara->IsPreScan());
    ui->radioButton_SweepScan->setChecked(pExpConllectPara->IsScan());
    if(pExpConllectPara->IsScan()==true)
    {
        on_radioButton_SweepScan_clicked();
    }
    else
    {
        on_radioButton_HopScan_clicked();
    }
    ui->spinBox_PreCount->setValue(pExpConllectPara->GetPreScanCount());
    ui->spinBox__MainCount->setValue(pExpConllectPara->GetScanCount());
    ui->lineEdit_PreTime->setText(QString::number(pExpConllectPara->GetPreScanTime()));
    ui->lineEdit_MainTime->setText(QString::number(pExpConllectPara->GetScanTime()));

    connect(ui->tableWidget_PreScan,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_PreScan_itemChanged(QTableWidgetItem*)));
    connect(ui->tableWidget_Hop,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Hop_itemChanged(QTableWidgetItem*)));
    connect(ui->tableWidget_Sweep,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Sweep_itemChanged(QTableWidgetItem*)));
    connect(this,SIGNAL(SIG_DataChanged()),this,SLOT(OnDataChanged()));

    pPreMyScanAxis->SetScan(Algorithm::GetSweepScanPara(pExpConllectPara->GetPreSweepScanParas())
                                                       ,pAnalyteDataBase->GetForbidRangs());
    if(ui->radioButton_HopScan->isChecked())
    {
        pMainMyScanAxis->SetScan(Algorithm::GetHopScanPara(pExpConllectPara->GetHopScanParas())
                                                        ,pAnalyteDataBase->GetForbidRangs());
    }
    else
    {
        pMainMyScanAxis->SetScan(Algorithm::GetSweepScanPara(pExpConllectPara->GetSweepScanParas())
                                                        ,pAnalyteDataBase->GetForbidRangs());
    }
}

void ExpCollectionParaFrame::on_checkBox_PreScan_clicked(bool checked)
{
    ui->tableWidget_PreScan->setEnabled(checked);
    ui->spinBox_PreCount->setEnabled(checked);
    ui->lineEdit_PreTime->setEnabled(checked);
}

void ExpCollectionParaFrame::on_radioButton_SweepScan_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    pMainMyScanAxis->SetScan(Algorithm::GetSweepScanPara(pExpConllectPara->GetSweepScanParas())
                                                        ,pAnalyteDataBase->GetForbidRangs());
//    if(ui->radioButton_HopScan->isChecked())
//    {
//        pMainMyScanAxis->SetScan(Algorithm::GetHopScanPara(pExpConllectPara->GetHopScanParas())
//                                                        ,pAnalyteDataBase->GetForbidRangs());
//    }
//    else
//    {
//        pMainMyScanAxis->SetScan(Algorithm::GetSweepScanPara(pExpConllectPara->GetSweepScanParas())
//                                                        ,pAnalyteDataBase->GetForbidRangs());
//    }
}

void ExpCollectionParaFrame::on_radioButton_HopScan_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    pMainMyScanAxis->SetScan(Algorithm::GetHopScanPara(pExpConllectPara->GetHopScanParas())
                                                        ,pAnalyteDataBase->GetForbidRangs());
//    if(ui->radioButton_HopScan->isChecked())
//    {
//        pMainMyScanAxis->SetScan(Algorithm::GetHopScanPara(pExpConllectPara->GetHopScanParas())
//                                                        ,pAnalyteDataBase->GetForbidRangs());
//    }
//    else
//    {
//        pMainMyScanAxis->SetScan(Algorithm::GetSweepScanPara(pExpConllectPara->GetSweepScanParas())
//                                                        ,pAnalyteDataBase->GetForbidRangs());
//    }
}

void ExpCollectionParaFrame::on_tableWidget_PreScan_itemChanged(QTableWidgetItem *item)
{
    SweepScanPara sweepScanPara;
    int column,row;
    int index;

    column=0;
    row=item->row();
    index=ui->tableWidget_PreScan->item(row,column++)->text().toInt();
    sweepScanPara.startMassNum=ui->tableWidget_PreScan->item(row,column++)->text().toDouble();
    sweepScanPara.endMassNum=ui->tableWidget_PreScan->item(row,column++)->text().toDouble();
    sweepScanPara.dwelltime=ui->tableWidget_PreScan->item(row,column++)->text().toDouble();
    sweepScanPara.channelsAmu=ui->tableWidget_PreScan->item(row,column++)->text().toInt();
    sweepScanPara.type=ui->tableWidget_PreScan->item(row,column++)->text();
    if(sweepScanPara.CheckValid()==false)
    {
        QMessageBox::information(this,tr("提示"),tr("参数输入不正确！"));
        LoadData();
        return;
    }

    pExpConllectPara->SetPreSweepScanPara(index,sweepScanPara);
    QList<SweepScanPara> sweepScanParas=pExpConllectPara->GetPreSweepScanParas();
    bool state=Algorithm::MergeSweepScanPara(pAnalyteDataBase->GetEnableRangs(),sweepScanParas,index);
    if(state==true)
    {
        pExpConllectPara->SetPreSweepScanParas(sweepScanParas);
        LoadData();
    }
    pPreMyScanAxis->SetScan(Algorithm::GetSweepScanPara(pExpConllectPara->GetPreSweepScanParas())
                                                       ,pAnalyteDataBase->GetForbidRangs());
}

void ExpCollectionParaFrame::on_tableWidget_Hop_itemChanged(QTableWidgetItem *item)
{
    HopScanPara hopScanPara;
    int column,row;

    column=0;
    row=item->row();
    hopScanPara.name=ui->tableWidget_Hop->item(row,column++)->text();
    hopScanPara.massNum=ui->tableWidget_Hop->item(row,column++)->text().toDouble();
    hopScanPara.dwelltime=ui->tableWidget_Hop->item(row,column++)->text().toDouble();
    hopScanPara.chanels=ui->tableWidget_Hop->item(row,column++)->text().toInt();
    hopScanPara.interval=ui->tableWidget_Hop->item(row,column++)->text().toDouble();
    hopScanPara.type=ui->tableWidget_Hop->item(row,column++)->text();
    if(hopScanPara.CheckValid()==false)
    {
        QMessageBox::information(this,tr("提示"),tr("参数输入不正确！"));
        LoadData();
        return;
    }

    pExpConllectPara->SetHopScanPara(hopScanPara.name,hopScanPara);
    pMainMyScanAxis->SetScan(Algorithm::GetHopScanPara(pExpConllectPara->GetHopScanParas())
                                                        ,pAnalyteDataBase->GetForbidRangs());
}

void ExpCollectionParaFrame::on_tableWidget_Sweep_itemChanged(QTableWidgetItem *item)
{
    SweepScanPara sweepScanPara;
    int column,row;
    QString name;
    int index;

    column=0;
    row=item->row();
    name=ui->tableWidget_PreScan->item(row,column++)->text();
    sweepScanPara.startMassNum=ui->tableWidget_Sweep->item(row,column++)->text().toDouble();
    sweepScanPara.endMassNum=ui->tableWidget_Sweep->item(row,column++)->text().toDouble();
    sweepScanPara.dwelltime=ui->tableWidget_Sweep->item(row,column++)->text().toDouble();
    sweepScanPara.channelsAmu=ui->tableWidget_Sweep->item(row,column++)->text().toInt();
    sweepScanPara.type=ui->tableWidget_Sweep->item(row,column++)->text(); 
    if(sweepScanPara.CheckValid()==false)
    {
        QMessageBox::information(this,tr("提示"),tr("参数输入不正确！"));
        LoadData();
        return;
    }

    pExpConllectPara->SetSweepScanPara(name,sweepScanPara);
    pMainMyScanAxis->SetScan(Algorithm::GetSweepScanPara(pExpConllectPara->GetSweepScanParas())
                                                        ,pAnalyteDataBase->GetForbidRangs());
}

void ExpCollectionParaFrame::OnElementChooseChanged(bool state,QString name)//add remove
{
    //qDebug()<<"state"<<state<<"name"<<name;
    SweepScanPara sweepScanPara;
    double value=pAnalyteDataBase->GetElementMass(name);
    sweepScanPara.SetStartEnd(value-0.1,value+0.1);
    sweepScanPara.name=name;
    HopScanPara hopScanPara;
    hopScanPara.name=name;
    hopScanPara.massNum=value;
    if(state==true)
    {
        pExpConllectPara->AddHopScanPara(name,hopScanPara);
        pExpConllectPara->AddSweepScanPara(name,sweepScanPara);
    }
    else
    {
        pExpConllectPara->RemoveHopScanPara(name);
        pExpConllectPara->RemoveSweepScanPara(name);
    }
}

void ExpCollectionParaFrame::OnDataChanged()
{
    pExpConllectPara->bPreScan=ui->checkBox_PreScan->isChecked();
    pExpConllectPara->bScan=ui->radioButton_SweepScan->isChecked();
    pExpConllectPara->preScanCount=ui->spinBox_PreCount->value();
    pExpConllectPara->scanCount=ui->spinBox__MainCount->value();
}
